<?php
	/**
	* 管理员类类
	* @author zhufu <zhufu159239@163.com>
	* @version 1.0
	* @date 2012-07-19 10:16:59
	*/
	class admin extends admincore{

		//管理员信息表
		private $table_admin = 'syt_admin';
		//管理员所在权限分组
		private $table_admin_group = 'syt_admin_group';
		//管理员 权限功能
		private $table_admin_module = 'syt_admin_module';
		//充值记录表
		private $table_admin_money_log = 'syt_money_log';
		
		/**
		* 构造函数
		*
		* @return void
		*/
		public function __construct() {

		}

		/**
		* 析构函数
		*
		* @return void
		*/
		public function __destruct() {


		}


		//===== 内部方法，需要登录才能操作  ===================================================================

		public function _pwd_edit($password, $old_pwd){
			//检查参数
			if(!$password || !$old_pwd){
				return false;
			}
			//得到当前用户Id
			$id = $this->_id();
			if(!$id){
				return false;
			}
			//加密密码
			$password = md5($password);
			$old_pwd = md5($old_pwd);
			//从数据库得到用户信息
			$row = $this->out_info_id($id);
			if($row['password'] != $old_pwd){
				return false;
			}
			$data = array('password'=>$password);
			if(!$this->out_update_admin($id, $data)){
				return false;
			}

			return true;
		}


		/**
		* 得到管理员是否登录
		* @author <zhufu159239@163.com> 2012-07-19 10:32:48
		*
		*/
		public function _is_login(){
			//得到用户登录信息
			$info = $this->out_cookie();
			if(!$info){
				return false;
			}
			return true;
		}


		/**
		* 得到管理员登录id
		* @author <zhufu159239@163.com> 2012-07-19 10:32:18
		*
		*/
		public function _id(){
			//得到用户登录信息
			$info = $this->out_cookie();
			if(!$info || !$info['id']){
				return false;
			}
			return $info['id'];
		}


		/**
		 * 充值		 
		 * @param unknown $data
		 */
		public function money_log($data){
			$data['time']=time();			
			$res= $this->set_db($this->table_admin_money_log, $data);			
			if(!$res){
				return false;
			}
			
			$sql="update {$this->table_admin} set `limit`=`limit`+{$data['code']} where id={$data['uid']}";			
			return $this->_mysql(0)->query($sql,true);			
		}
		

        /**
         * 查看充值日志信息
         * @param unknown $data
         */
		public function money_log_search($data,$page=1,$pagesize=10){
			$sql = "SELECT * FROM {$this->table_admin_money_log} WHERE 1 ";
			
			if($data['uid']){
				 $sql .= " AND uid={$data['uid']} " ;
			}
			
			$sql .= " ORDER BY id DESC ";
			return $this->_mysql(0)->findlimit($sql,$page,$pagesize);
		}
		
		
		
		//===== 外部方法，操作不需要登录  ===================================================================

		/**
		* 裁剪上传的图片
		* @author zhufu 2013-06-03
		*
		* @param string $path 图片路径,原图，相对地址
		* @param int $width 宽度
		* @param int $height 高度
		* @param int $inc_ary 更多参数1-fill_margin2-cut_center3-quality
		* @param int $exts  图片后缀
		*/
		public function image_process($path, $width, $height, $inc_ary=false,$exts=''){
			$tmp = $this->_root() . $path;
			//图片后缀
			$ext = $exts ? $exts : "{$width}x{$height}";
			$tmp2 = img_thumb($tmp, $ext);

			if(is_file($tmp2)){
				return true;
			}
			$pathinfo = pathinfo($tmp);
			$ext_adv = "_{$ext}";
			$tmp = array('quality'=>90,'fill_margin'=>false,'cut_center'=>false);
			if($inc_ary){
				$tmp = array_merge($tmp, $inc_ary);
			}
			return $this->_file()->image_make_thumb_adv($pathinfo['dirname'], $pathinfo['basename'], 0,0,0,0,$width,$height,'',$tmp, $pathinfo['basename'].$ext_adv.".{$pathinfo['extension']}");
		}


		/**
		* 退出登录
		* @author <zhufu159239@163.com> 2012-07-14 11:59:51
		*
		*/
		public function out_quit(){
			//没有 cookie 信息，直接返回 false
			if(!$this->out_cookie()){
				return false;
			}
			//清除 session
			$_SESSION = array();
			session_destroy();
			//清除 cookie
			$config = $this->_config();
			del_cookie($config['cookie']['prefix'].'admin');
			del_cookie($config['cookie']['prefix'].'admin', $config['cookie']['domain']);

			return true;
		}


		/**
		* 得到管理员的登录信息
		* @author <zhufu159239@163.com> 2012-07-19 10:33:14
		*
		*/
		public function out_cookie(){
			//得到配置
			$config = $this->_config();
			
			//得到 cookie
			$info = get_cookie($config['cookie']['prefix'] . 'admin');			
			$info = $this->userinfo_decode($info);
			if(!$info){
				return false;
			}
			return $info;
		}

		public function getTime($id){
			$sql="select `limit` from {$this->table_admin} where id={$id}";
			$res=$this->_mysql(0)->findone($sql);
			
			return empty($res['limit'])?0:$res['limit'];
		}
		
		public function chTime($id){
			$sql="update {$this->table_admin} set `limit`=`limit`-1,limited=limited+1 where id={$id}";
			$this->_mysql(0)->query($sql,true);
		}


		/**
		* 根据账号得到管理员信息
		* @author <zhufu159239@163.com> 2012-07-19 11:26:15
		*
		* @param string $account    账号
		* @return array
		*/
		public function out_info_account($account){
			//检查参数
			if(!$account){
				return false;
			}
			//查询信息
			$sql = "SELECT * FROM {$this->table_admin} WHERE `account`='{$account}'";
			$row = $this->_mysql(0)->findone($sql);
			if(!$row){
				return false;
			}
			return $row;
		}



		/**
		* 根据id,得到管理员信息
		* @author <zhufu159239@163.com> 2012-07-19 11:27:23
		*
		* @param int $id    管理员id
		* @return array
		*/
		public function out_info_id($id,$cols="*"){
			//检查参数
			if(!$id){
				return false;
			}
			//查询信息
			$sql = "SELECT {$cols} FROM {$this->table_admin} WHERE `id`='{$id}'";
			$row = $this->_mysql(0)->findone($sql);
			if(!$row){
				return false;
			}
			return $row;
		}

		/**
		* 根据电话得到管理员信息
		* @author limin 2013-8-6  PM 13:09:00
		* @param  string $phone 电话
		* @return array
		*/
		public function out_info_phone($phone){
			//检查参数
			if(!is_string($phone)){
				return false;
			}
			//查询信息
			$sql = "SELECT * FROM {$this->table_admin} WHERE `phone`='{$phone}'";
			$row = $this->_mysql(0)->findone($sql);
			if(!$row){
				return false;
			}
			return $row;
		}

		/**
		* 管理员登录
		* @author <zhufu159239@163.com> 2012-07-19 11:22:37
		*
		* @param string $account    管理员账号
		* @param string $password   管理员密码
		* @param int $group 登录的组号
		*/
		public function out_login($account, $password, $group=0){
			//检查参数
			if(!$account || !$password){
				return false;
			}
			$password = md5($password); //密码加密

			//得到管理员信息
			$row = $this->out_info_account($account);
			if(!$row || $row['password'] != $password){
				return false;
			}
			if($group && $row['group'] != $group){
				return false;
			}
			//===  账号密码匹配  =======================
			//组合将要写入 cookie 的数据
			//得到配置
			$config = $this->_config();

			/**
			* 通过权限组得到权限
			* 并该用户拥有的权限  传到cookie中
			* yufu 2012-7-21
			*/
			$result = $this->out_find_group($row['group']);
			$row['admin_module'] = $result['type_module'];

			//记录COOKIE
			$info = $this->userinfo_encode($row);
			set_cookie($config['cookie']['prefix'] . 'admin', $info, 7, $config['cookie']['path']);

			$data_login= array(
			'login_ip'=>get_remote_ip(),
			'login_time'=>time()
			);
			$this ->out_update_admin($row['id'], $data_login);
			
			/* 记录登录SESSION和COOKIES */
			$auth = array(
				'uid'       => $row['id'],
				'account'   => $row['account'],
				'login_time'=> time()
			);
									
			set_cookie('admin', data_auth_sign($auth));			
			set_cookie('admin_info', base64_encode(serialize($auth)));						

			//写入登录日志
			//$data = array('content'=>serialize(array('ip'=>get_remote_ip())));
			//$this->adminlogs->out_write($this->adminlogs->type_ary['login'], $data, $row['id']);

			return $row;
		}

		/**
		* 查询所拥有的权限
		*
		* @author  yufu 2012-7-19
		* @param int $type_group  拥有的权限
		*/
		public function out_find_group($id){
			if(!is_numeric($id)){
				return false;
			}
			//这里涉及到登陆时候查询权限  不用检查是否登陆
			$sql = "select * from {$this->table_admin_group} where id = {$id}";

			return $this->_mysql(0)->findone($sql);
		}
		/**
		* 查询 管理员功能模块分组
		*
		* @author  yufu 2012-7-19
		* @param int $arr_id   功能模块id
		*/
		public function out_find_module($arr_id){
			if(!is_string($arr_id)){
				return false;
			}
			if(!$this->_id()){
				return false;
			}
			$sql ="select * from {$this->table_admin_module} where id in({$arr_id}) and state=0 ORDER BY id ASC";


			return $this->_mysql(0)->find($sql);
		}
		/**
		* 帐号唯一性检测
		* @author xiezhihui 2014-5-13 14:28:54
		* @param mixed $account
		* @return mysql对象
		*/
		public function unique_admin($account){
			$sql ="select * from {$this->table_admin} where account = '{$account}'";
			return $this->_mysql(0)->findone($sql);
		}

		/**
		* 通过id 查询单个的管理员
		*
		* @author  yufu 2012-7-19 upate by zhufu
		* @param int $id  管理员id
		*/
		public function out_find_admin($id){
			if(!is_numeric($id)){
				return false;
			}
			if(!$this->_id()){
				//自身没有登录，无权查看管理员信息
				return false;
			}
			$sql ="select * from {$this->table_admin} where id ={$id}";

			return $this->_mysql(0)->findone($sql);
		}

		/**
		* 查询所有的权限 便于管理
		*
		* @author  yufu 2012-7-19
		*/
		public function out_find_module_list(){
			if(!$id=$this->_id()){
				return false;
			}
			$sql ="select * from {$this->table_admin_module} ORDER BY parent_id ASC, sort ASC";
			return $this->_mysql(0)->find($sql);
		}

		/**
		* 查询所有 权限分组
		*
		* @author  yufu 2012-7-19
		*/
		public function out_find_group_list($data=array(),$page=1,$pagesize=10){
			if(!$this->_id()){
				return false;
			}

			$sql ="select * from {$this->table_admin_group}";
			if($data['limit']==true){
				return $this->_mysql(0)->findlimit($sql,$page,$pagesize);
			}
			return $this->_mysql(0)->find($sql);
		}

		/**
		* 获得管理员信息
		* @author limin 2013-7-5  PM 16:37:00
		* @param  array $where  搜索数组
		*/
		public function _list_by_group($where){
			//登录判断
			if(!$this->_id()){
				return false;
			}
			//组织sql语句
			$sql="SELECT * FROM {$this->table_admin} WHERE state=1 ";
			if($where['city']){
				$sql.=" and `city`={$where['city']} ";
			}
			if($where['area']){
				$sql.=" and `area`={$where['area']} ";
			}
			if($where['group']){
				$sql.=" and `group`={$where['group']} ";
			}
			return $this->_mysql(0)->find($sql);
		}

		/**
		* 查询用户 列表
		* @author  yufu 2012-7-19
		*
		* @param   int @groupid 所属组id
		* @param int $page  当前页
		* @param int $pagesize  每页数量
		*/
		public function out_find_admin_list($groupid, $page=1, $search=array(), $pagesize=10){
			if(!$this->_id()){
				return false;
			}
			if(!is_numeric($page) || !is_numeric($pagesize)){
				return false;
			}

			$sql ="select a.*,a2.name as create_name from {$this->table_admin} as a left join {$this->table_admin} as a2 on a.create_id=a2.id where a.group={$groupid}";
			if($search['account']){
				$sql .= " AND a.account LIKE '%{$search['account']}%'";
			}
			if(is_numeric($search['state']) && $search['state']>=0){
				$sql .= " AND a.state={$search['state']}";
			}

			if($search['kws']){
				//关键字搜索
				$sql .= " AND (a.name LIKE '%{$search['kws']}%' OR a.account LIKE '%{$search['kws']}%')";
			}
			//性别搜索
			if(is_numeric($search['sex'])){
				$sql .= " AND a.sex={$search['sex']}";
			}
			if($search['y_phone']){
				$sql.=" AND a.y_phone='{$search['y_phone']}'";
			}

			//注册时间搜索
			if($search['start_time']){
				$sql .= " AND a.time>={$search['start_time']}";
			}
			if($search['end_time']){
				$sql .= " AND a.time<={$search['end_time']}";
			}
			if($search['create_id']){
				if($search['dls_id']){
					$sql .= " AND (a.create_id={$search['create_id']} OR a.dls_id={$search['dls_id']})";
				}
				else{
					$sql .= " AND a.create_id={$search['create_id']}";
				}
			}
			$sql .= " order by a.id DESC";
			return $this->_mysql(0)->findlimit($sql , $page, $pagesize);
		}


		/**
		* 得到管理员列表
		* @author limin 2013-9-21  AM 9:28:00
		* @param  array  $data 搜索数组
		*/
		public function _list($data,$page=1,$pagesize=10){
			//判断是否登录
			if(!$uid = $this->_id()){
				return false;
			}
			//组织sql语句
			$sql="SELECT a.*,b.content as groupname FROM {$this->table_admin} as a ,{$this->table_admin_group} as b WHERE a.`group` = b.id";
			if($data['group']){
				$sql.=" AND a.`group`={$data['group']} ";
			}
			
			//子账号查询
			if(is_numeric($data['child'])){
				$sql.=" AND (a.master>0 AND a.master={$data['child']} AND a.group=12)";
			}
			
			//后台经理、管理员账号查询
			if($data['admin_manage']){
				$sql.=" AND a.master=0";
			}
			
			//项目经理账号
			if($data['master']){
				$sql.=" AND (a.group=10 AND a.master=0)";
			}
			
			if($data['create_id']){
				$sql .= " AND a.create_id={$data['create_id']}";
			}
			if($data['state']==1){
				$sql .= " AND a.state=1";
			}
			if($data['limit'] === true){
				return $this->_mysql(0)->findlimit($sql,$page,$pagesize);
			}
			return $this->_mysql(0)->find($sql);
		}
		
		
		function search_customer($cols="*"){
			$sql="SELECT {$cols} FROM {$this->table_admin} WHERE id<>1";
			return $this->_mysql(0)->find($sql);
		}		
		
		
		function search_manage($cols="*"){
			$sql="SELECT {$cols} FROM {$this->table_admin} WHERE id<>1 and master=0 and state=1";
			return $this->_mysql(0)->find($sql);
		}		
		
		
		/**
		* 获得管理员列表
		* @author xiezhihui 2013-12-27 AM 15:38:00
		* @param  array  $data 搜索数组
		*/
		public function _list_by_proxy($data,$page=1,$pagesize=10){
			//判断是否登录
			if(!$uid = $this->_id() || $uid['group']!=7){
				return false;
			}
			//组织sql语句
			$sql="SELECT a1.*,a2.account as proxy_name, a2.id as id2 FROM {$this->table_admin} as a1 ,{$this->table_admin} as a2 WHERE 1 ";
			$sql.=" AND a1.group=2 AND a2.group=7 AND a1.create_id=a2.id";

			if($data['state']==1){
				$sql .= " AND a1.state=1";
			}
			if($data['limit'] === true){
				return $this->_mysql(0)->findlimit($sql,$page,$pagesize);
			}
			return $this->_mysql(0)->find($sql);
		}


		/**
		* 添加管理员
		*
		* @author  yufu 2012-7-19
		* @param array $data
		*/
		public function add_admin($data){
			if(empty($data)){
				return false;
			}

			if(!$uid = $this->_id()){
				return false;
			}
			//创建人id
			$data['create_id'] = $uid ;
			//创建人ip
			$data['ip']  = get_remote_ip();
			//创建时间
			$data['time']  = time();
			return $this->set_db($this->table_admin, $data);

		}


		/**
		* 添加一个管理 模块组
		*
		* @author  yufu 2012-7-19
		* @param array $data
		*/
		public function add_group($data){
			if(empty($data)){
				return false;
			}
			if(!$uid = $this->_id()){
				return false;
			}
			return $this->set_db($this->table_admin_group, $data);

		}

		/**
		* 添加管理员权限类别
		*
		* @author  yufu 2012-7-19
		* @param array $data
		*/
		public function add_module($data){
			if(empty($data)){
				return false;
			}
			//检查是否登录
			if(!$this->_id()){
				return false;
			}

			return $this->set_db($this->table_admin_module, $data);


		}


		/**
		* 修改 管理员 信息登陆
		* @author  yufu 2012-7-19
		* @param mixed $data
		*/
		public function update_admin($data){
			if(empty($data)){
				return false;
			}
			
			//检查是否登录
			if(!$this->_id()){
				return false;
			}
			$wherestr = " id={$data['id']}";			
			return $this->set_db($this->table_admin, $data,false,$wherestr);
		}
		/**
		* 外部修改 管理员 信息登陆
		* @author  yufu 2012-7-19 upate by zhufuupd
		*
		* @param int $id        要修改的管理员id
		* @param array $data    要修改的数据
		*/
		private function out_update_admin($id, $data){
			if(empty($data)){
				return false;
			}
			$wherestr = " id={$id}";
			return $this->set_db($this->table_admin, $data,false,$wherestr);
		}

		/**
		* 修改一个模块
		* @author  yufu 2012-7-19
		* @param mixed $data
		*/
		public function update_group($data){
			if(empty($data)){
				return false;
			}
			//检查是否登录
			if(!$this->_id()){
				return false;
			}
			$wherestr = " id={$data['id']}";
			return $this->set_db($this->table_admin_group, $data,false,$wherestr);
		}

		/**
		* 根据上级id,得到下级的权限列表
		* @author <zhufu159239@163.com> 2012-08-06 15:19:31
		*
		* @param int $pid   上级id
		* @return array
		*/
		public function out_module_list_pid($pid=0){
			if(!is_numeric($pid)){
				return false;
			}
			$sql = "SELECT * FROM {$this->table_admin_module} WHERE parent_id={$pid} ORDER BY sort ASC";
			return $this->_mysql(0)->find($sql);
		}



		/**
		* 修改 管理员权限类别
		*
		* @author  yufu 2012-7-19  upate by zhufu
		* @param int $id        要修改的记录id
		* @param array $data    要修改的数据
		*/
		public function update_module($id, $data){
			if(empty($data) || !$id){
				return false;
			}
			//检查是否登录
			if(!$this->_id()){
				return false;
			}
			$wherestr = " id={$id}";
			return $this->set_db($this->table_admin_module, $data,false,$wherestr);
		}


		/**
		* 获取数据库大小
		*
		* @author zhouqi 2012-9-11 PM 04:45:52
		*/
		public function _database_size() {
			if(!$uid = $this->_id()) {
				return false;
			}

			$_config = $this->_config();

			$sql = 'SELECT sum(data_length + index_length )/1024/1024 as datasize FROM information_schema.TABLES where table_schema="' . $_config['db'][0]['database'] . '" GROUP BY table_schema';

			$row = $this->_mysql(0)->findone($sql);
			if(!$row || empty($row)) {
				return 0;
			}

			return number_format($row['datasize'], 2);
		}


	}
?>